package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgMainNavigationBO;
import com.ztgf.shopping.models.bo.epg.EpgMainNavigationSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgMainNavigationDTO;
import com.ztgf.shopping.models.service.epg.IEpgMainNavigationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 主导航控制器
 *
 * @author YUE
 * @create 2020-05-18 11:36*/



@RestController
@RequestMapping("/cms/epgMainNavigation")
@Slf4j
@Api(value = "EpgMainNavigationController",tags = "主导航接口")
public class EpgMainNavigationController {

    @DubboReference
    private IEpgMainNavigationService iEpgMainNavigationService;

    @ApiOperation(value = "添加主导航",notes = "添加主导航")
    @PostMapping(value = "/insert")
    public Result insert( EpgMainNavigationSaveBO epgMainNavigationSaveBO) {
        if(null == epgMainNavigationSaveBO.getSort())
            return new Result<>(CodeEnum.FAIL_100033);
        boolean flag = iEpgMainNavigationService.add(epgMainNavigationSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存主导航",notes = "保存主导航")
    public Result save( EpgMainNavigationSaveBO epgMainNavigationSaveBO){
        if(epgMainNavigationSaveBO.getId() == null){
            //添加
            return  insert(epgMainNavigationSaveBO);
        } else {
            return  update(epgMainNavigationSaveBO);
        }
    }

    @ApiOperation(value = "修改主导航",notes = "修改主导航")
    @PostMapping("/update")
    public Result update( EpgMainNavigationSaveBO epgMainNavigationSaveBO){
        EpgMainNavigationDTO epgMainNavigationDTO = new EpgMainNavigationDTO();
        BeanUtils.copyProperties(epgMainNavigationSaveBO, epgMainNavigationDTO);
        epgMainNavigationDTO.setId(epgMainNavigationSaveBO.getId());
        boolean flag = iEpgMainNavigationService.updateById(epgMainNavigationDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除主导航",notes = "删除主导航")
    @ApiImplicitParam(name = "id",value = "字典表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iEpgMainNavigationService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除主导航",notes = "批量删除主导航")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iEpgMainNavigationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgMainNavigationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        EpgMainNavigationDTO epgMainNavigationDTO = iEpgMainNavigationService.selectById(id);
        return new Result<>(epgMainNavigationDTO);
    }

    @ApiOperation(value = "主导航列表",notes = "主导航列表")
    @PostMapping("/querySelectPageList")
    public Result<Page<EpgMainNavigationDTO>> querySelectPageList( EpgMainNavigationBO epgMainNavigationBO){
        if(null == epgMainNavigationBO)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgMainNavigationDTO> page = iEpgMainNavigationService.getBySelectMainNavigationInfoPage(epgMainNavigationBO);
        return new Result<>(page);
    }
}